<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Review & Rating Synthesis
 * @version 	1.0.2
 * @author 		Rukan <rukanhitokiri@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://www.zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
 
include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');

global $cookie;

if (isset($_GET['deteteComment'])) {
	$id_zproduct_review =  Tools::getValue('id');
	if (Db::getInstance(_PS_USE_SQL_SLAVE_)->Execute('DELETE FROM `'._DB_PREFIX_.'zproduct_review` 
													WHERE (`id_zproduct_review`='.$id_zproduct_review.')'))
		echo $id_zproduct_review;
	else {
		echo '0';
	} 
}

if (isset($_GET['vote'])) {
	if ($cookie->isLogged() == true){
		if (Tools::getValue('vote') == 'like' && $cookie->id_customer) {
			$row = Db::getInstance()->getRow('SELECT `vlike`,`vdislike` FROM `'._DB_PREFIX_.'zproduct_review`
												WHERE `id_zproduct_review` ='.(int)Tools::getValue('id'));
			$vdislikeArray = explode(",", trim($row['vdislike'], ","));
			if (!in_array($cookie->id_customer, $vdislikeArray)) {
				if (empty($row['vlike'])) {
					$numberLike = 1;
					Db::getInstance()->Execute(
						'UPDATE `'._DB_PREFIX_.'zproduct_review` 
							SET vlike=concat(vlike,"'.$cookie->id_customer.',"), number_like = number_like + 1 
								WHERE `id_zproduct_review` ='.(int)Tools::getValue('id'));
				} else {
					$vlikeArray = explode(",", trim($row['vlike'], ","));
					$numberLike = count($vlikeArray);
					if (!in_array($cookie->id_customer, $vlikeArray)) {
						Db::getInstance()->Execute(
							'UPDATE `'._DB_PREFIX_.'zproduct_review` 
								SET vlike=concat(vlike,"'.$cookie->id_customer.',") 
									WHERE `id_zproduct_review` ='.(int)Tools::getValue('id'));
						$numberLike = $numberLike +1;
					}else{
						die('a');
					}
				}	
				die ("$numberLike");
			}else {
				die('b');
			}
			
		} else if (Tools::getValue('vote') == 'dislike' && $cookie->id_customer) {
			$row = Db::getInstance()->getRow('SELECT `vlike`,`vdislike` FROM `'._DB_PREFIX_.'zproduct_review`
												WHERE `id_zproduct_review` ='.(int)Tools::getValue('id'));
			$vlikeArray = explode(",", trim($row['vlike'], ","));
			if (!in_array($cookie->id_customer, $vlikeArray)) {
				if (empty($row['vdislike'])) {
					$numberDislike = 1;
					Db::getInstance()->Execute(
						'UPDATE `'._DB_PREFIX_.'zproduct_review` 
							SET vdislike=concat(vdislike,"'.$cookie->id_customer.',") 
								WHERE `id_zproduct_review` ='.(int)Tools::getValue('id'));
				} else {
					$vdislikeArray = explode(",", trim($row['vdislike'], ","));
					$numberDislike = count($vdislikeArray);
					if (!in_array($cookie->id_customer, $vdislikeArray)) {
						Db::getInstance()->Execute(
							'UPDATE `'._DB_PREFIX_.'zproduct_review` 
								SET vdislike=concat(vdislike,"'.$cookie->id_customer.',") 
									WHERE `id_zproduct_review` ='.(int)Tools::getValue('id'));
						$numberDislike = $numberDislike +1;
					}else{
						die('a');
					}
				}	
				die ("$numberDislike");
			}else {
				die('b');
			}
		}
	}else{
		die('notLogged');
	}
	die ('error');
}

